using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using Model;
using System.Text;

public partial class NewIdeaForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["uid"] == null)
                Response.Redirect("auth.aspx");
        }
    }

    protected void Lbtn_Click(object sender, EventArgs e)
    {
        int i = int.Parse(Request.QueryString["i"]);
        string path = "";
        switch (i)
        {
            case 0:
                path = "Hot.aspx";
                break;
            case 1:
                path = "Latest.aspx";
                break;
            case 2:
                path = "My.aspx";
                break;
            case 3:
                path = "Favorite.aspx";
                break;
        }
        Response.Redirect(path);
    }

    protected void Ibtn_Click(object sender, ImageClickEventArgs e)
    {
        int i = int.Parse(Request.QueryString["i"]);
        string path = "";
        switch (i)
        {
            case 0:
                path = "Hot.aspx";
                break;
            case 1:
                path = "Latest.aspx";
                break;
            case 2:
                path = "My.aspx";
                break;
            case 3:
                path = "Favorite.aspx";
                break;
        }
        Response.Redirect(path);
    }

    //protected void btnUpload_Click(object sender, EventArgs e)
    //{
    //    try
    //    {
    //        // Get the HttpFileCollection
    //        HttpFileCollection hfc = Request.Files;
    //        string attachxmlstr = "";
    //        for (int i = 0; i < hfc.Count; i++)
    //        {
    //            if (!string.IsNullOrEmpty(hfc[i].FileName))
    //            {
    //                string[] tmps = hfc[i].FileName.Split('\\');
    //                string tmpname = Session["uid"].ToString() + "_" + System.DateTime.Now.ToString("yyyy-MM-dd") + "-" + System.DateTime.Now.Hour.ToString() + "-" + System.DateTime.Now.Minute.ToString() + "-" + System.DateTime.Now.Second.ToString() + System.IO.Path.GetExtension(hfc[i].FileName);
    //                attachxmlstr += tmpname + "|";
    //                HttpPostedFile hpf = hfc[i];
    //                if (hpf.ContentLength > 0)
    //                {
    //                    hpf.SaveAs(Server.MapPath("UploadFiles") + "\\" + tmpname);
    //                }
    //            }
    //        }

    //        if (attachxmlstr.Length > 0)
    //        {
    //            if (ViewState["attach"] != null)
    //                ViewState["attach"] += "|";
    //            ViewState["attach"] += attachxmlstr.Remove(attachxmlstr.Length - 1, 1);
    //        }
    //    }
    //    catch (Exception ex)
    //    {
    //        throw ex;
    //    }
    //}

    protected void Sbtn_ServerClick(object sender, ImageClickEventArgs e)
    {
        // Upload File
        try
        {
            // Get the HttpFileCollection
            HttpFileCollection hfc = Request.Files;
            string attachxmlstr = "";
            for (int i = 0; i < hfc.Count; i++)
            {
                if (!string.IsNullOrEmpty(hfc[i].FileName))
                {
                    string[] tmps = hfc[i].FileName.Split('\\');
                    string tmpname = Session["uid"].ToString() + "_" + System.DateTime.Now.ToString("yyyy-MM-dd") + "-" + System.DateTime.Now.Hour.ToString() + "-" + System.DateTime.Now.Minute.ToString() + "-" + System.DateTime.Now.Second.ToString() + System.IO.Path.GetExtension(hfc[i].FileName);
                    attachxmlstr += tmpname + "|";
                    HttpPostedFile hpf = hfc[i];
                    if (hpf.ContentLength > 0)
                    {
                        hpf.SaveAs(Server.MapPath("UploadFiles") + "\\" + tmpname);
                    }
                }
            }

            if (attachxmlstr.Length > 0)
            {
                if (ViewState["attach"] != null)
                    ViewState["attach"] += "|";
                ViewState["attach"] += attachxmlstr.Remove(attachxmlstr.Length - 1, 1);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

        //Add new idea

        IdeaService isc = new IdeaService();
        Idea idea = new Idea();
        idea.Title = Caption.Value;
        idea.Content = Content.InnerText;
        idea.CategoryType = int.Parse(CategoryType.Value);
        idea.Votes = 0;
        idea.Status = 1;
        string hour = System.DateTime.Now.Hour > 9 ? System.DateTime.Now.Hour.ToString() : "0" + System.DateTime.Now.Hour;
        string min = System.DateTime.Now.Minute > 9 ? System.DateTime.Now.Minute.ToString() : "0" + System.DateTime.Now.Minute;
        string sec = System.DateTime.Now.Second > 9 ? System.DateTime.Now.Second.ToString() : "0" + System.DateTime.Now.Second;
        idea.CreatedTime = System.DateTime.Now.ToString("yyyy-MM-dd") + " " + hour + ":" + min + ":" + sec;
        idea.Attachments = ViewState["attach"] == null ? "" : ViewState["attach"].ToString();
        idea.CreatedBy = Session["uid"].ToString();

        isc.CreateIdea(idea);

        //Send Email
        string subject = "";
        string body = "";
        //Utilities.SendMail("",subject, body);
        int ipage = int.Parse(Request.QueryString["i"]);
        string path = "";
        switch (ipage)
        {
            case 0:
                path = "Hot.aspx";
                break;
            case 1:
                path = "Latest.aspx";
                break;
            case 2:
                path = "My.aspx";
                break;
            case 3:
                path = "Favorite.aspx";
                break;
        }
        Response.Redirect("ResponsePage.aspx?type=2&path=" + path);
    }
}
